﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TecTools;
using TecIntern.Properties;
using TecExtensions;

namespace TecIntern.GUI {
    public partial class SelectForm : Form {

        #region Members

        private Extensions.tpContent contentType;
        private Extensions.tpSorting sortingType;
        private Extensions.tpSortOrder sortOrder;
        private Extensions.tpShow showType;

        #endregion

        #region Constructor

        public SelectForm() {
            InitializeComponent();
        }

        #endregion

        #region Properties

        public bool Cancel {
            get;
            set;
        }

        public DateTime DateFrom {
            get;
            set;
        }

        public DateTime DateTo {
            get;
            set;
        }

        public Extensions.tpContent Content {
            get { return contentType; }
            set { this.contentType = value; }
        }

        public Extensions.tpSorting Sorting {
            get { return sortingType; }
            set { this.sortingType = value; }
        }

        public Extensions.tpSortOrder SortOrder {
            get { return sortOrder; }
            set { this.sortOrder = value; }
        }

        public Extensions.tpShow ShowType {
            get { return showType; }
            set { this.showType = value; }
        }

        #endregion

        #region Methods

        private void LoadDefaultValues() {
            this.rbAll.Checked = Settings.Default.DefaultSelectAll;
            this.rbDay.Checked = Settings.Default.DefaultSelectDay;
            this.rbMonth.Checked = Settings.Default.DefaultSelectMonth;
            this.rbWeek.Checked = Settings.Default.DefaultSelectWeek;
            this.rbYear.Checked = Settings.Default.DefaultSelectYear;

            this.rbContentAll.Checked = Settings.Default.DefaultContentAllValue;
            this.rbContentProject.Checked = Settings.Default.DefaultContentProjectsValue;
            this.rbContentIntern.Checked = Settings.Default.DefaultContentJobsValue;

            this.rbSortingArea.Checked = Settings.Default.DefaultSortingAreaValue;
            this.rbSortingDate.Checked = Settings.Default.DefaultSortingDateValue;
            this.rbSortingHour.Checked = Settings.Default.DefaultSortingHourValue;

            this.rbSortDown.Checked = Settings.Default.DefaultSortOrderDownValue;
            this.rbSortUp.Checked = Settings.Default.DefaultSortOrderUpValue;

            this.rbShowArea.Checked = Settings.Default.DefaultShowAreaValue;
            this.rbShowList.Checked = Settings.Default.DefaultShowListValue;

            this.dtFrom.Value = DateTime.Now.AddYears(-1);
            this.dtTo.Value = DateTime.Now;
        }

        private void SaveDefaultValues() {
            Settings.Default.DefaultSelectDay = this.rbDay.Checked;
            Settings.Default.DefaultSelectMonth = this.rbMonth.Checked;
            Settings.Default.DefaultSelectWeek = this.rbWeek.Checked;
            Settings.Default.DefaultSelectYear = this.rbYear.Checked;
            Settings.Default.DefaultSelectAll = this.rbAll.Checked;

            Settings.Default.DefaultContentAllValue = this.rbContentAll.Checked;
            Settings.Default.DefaultContentJobsValue = this.rbContentIntern.Checked;
            Settings.Default.DefaultContentProjectsValue = this.rbContentProject.Checked;

            Settings.Default.DefaultShowAreaValue = this.rbShowArea.Checked;
            Settings.Default.DefaultShowListValue = this.rbShowList.Checked;

            Settings.Default.DefaultSortingAreaValue = this.rbSortingArea.Checked;
            Settings.Default.DefaultSortingDateValue = this.rbSortingDate.Checked;
            Settings.Default.DefaultSortingHourValue = this.rbSortingHour.Checked;

            Settings.Default.DefaultSortOrderUpValue = this.rbSortUp.Checked;
            Settings.Default.DefaultSortOrderDownValue = this.rbSortDown.Checked;

            Settings.Default.DefaultFromDate = this.dtFrom.Value;
            Settings.Default.DefaultToDate = this.dtTo.Value;
            Settings.Default.Save();
        }

        private void SetDataBeforeClose() {
            DateTo = this.dtTo.Value;
            SaveDefaultValues();
        }

        private void SetDates() {

            DateFrom = new DateTime(DateTime.Today.AddYears(-10).Year, 1, 1);
            this.dtFrom.Value = DateTime.Today;

            #region Select Time

            if(this.rbDay.Checked) {
                this.dtTo.Value = DateTime.Today;
            }
            if(this.rbWeek.Checked) {
                this.dtTo.Value = Tools.GetLastDayOfWeek(DateTime.Today);
            }
            if(this.rbMonth.Checked) {
                this.dtTo.Value = Tools.GetLastDayOfMonth(DateTime.Today);
            }
            if(this.rbYear.Checked) {
                this.dtTo.Value = Tools.GetLastDayOfYear(DateTime.Today);
            }
            if(this.rbAll.Checked) {
                this.dtFrom.Value = new DateTime(DateTime.Today.AddYears(-10).Year, 1, 1);
                this.dtTo.Value = new DateTime(DateTime.Today.AddYears(10).Year, 12, 31);
            }

            #endregion

            #region Content

            if(this.rbContentAll.Checked) {
                this.Content = Extensions.tpContent.AllJobs;
            }

            if(this.rbContentIntern.Checked) {
                this.Content = Extensions.tpContent.InternJobs;
            }
            if(this.rbContentProject.Checked) {
                this.Content = Extensions.tpContent.ProjectJobs;
            }

            #endregion

            #region Sorting

            if(this.rbSortingArea.Checked) {
                this.Sorting = Extensions.tpSorting.Area;
            }

            if(this.rbSortingDate.Checked) {
                this.Sorting = Extensions.tpSorting.Date;
            }

            if(this.rbSortingHour.Checked) {
                this.Sorting = Extensions.tpSorting.Hour;
            }

            #endregion

            #region SortOrder

            if(this.rbSortUp.Checked) {
                this.SortOrder = Extensions.tpSortOrder.Up;
            }

            if(this.rbSortDown.Checked) {
                this.SortOrder = Extensions.tpSortOrder.Down;
            }

            #endregion

            #region Show

            if(this.rbShowArea.Checked) {
                this.ShowType = Extensions.tpShow.Area;
            }

            if(this.rbShowList.Checked) {
                this.ShowType = Extensions.tpShow.List;
            }

            #endregion

        }

        #endregion

        #region Events

        private void SelectForm_Load(object sender, EventArgs e) {
            LoadDefaultValues();
            Cancel = true;

        }

        private void rbDay_CheckedChanged(object sender, EventArgs e) {
            SetDates();
        }

        private void rbWeek_CheckedChanged(object sender, EventArgs e) {
            SetDates();
        }

        private void rbMonth_CheckedChanged(object sender, EventArgs e) {
            SetDates();
        }

        private void rbYear_CheckedChanged(object sender, EventArgs e) {
            SetDates();
        }

        private void rbAll_CheckedChanged(object sender, EventArgs e) {
            SetDates();
        }

        private void dtFrom_ValueChanged(object sender, EventArgs e) {
            DateFrom = this.dtFrom.Value;

        }

        private void dtTo_ValueChanged(object sender, EventArgs e) {
            DateTo = this.dtTo.Value;
        }

        private void rbContentAll_CheckedChanged(object sender, EventArgs e) {
            SetDates();
        }

        private void rbContentProject_CheckedChanged(object sender, EventArgs e) {
            SetDates();
        }

        private void rbContentIntern_CheckedChanged(object sender, EventArgs e) {
            SetDates();
        }

        private void rbSortingDate_CheckedChanged(object sender, EventArgs e) {
            SetDates();
        }

        private void rbSortingArea_CheckedChanged(object sender, EventArgs e) {
            SetDates();
        }

        private void rbSortingHour_CheckedChanged(object sender, EventArgs e) {
            SetDates();
        }

        private void rbSortUp_CheckedChanged(object sender, EventArgs e) {
            SetDates();
        }

        private void rbSortDown_CheckedChanged(object sender, EventArgs e) {
            SetDates();
        }

        private void rbShowArea_CheckedChanged(object sender, EventArgs e) {
            SetDates();
        }

        private void rbShowList_CheckedChanged(object sender, EventArgs e) {
            SetDates();
        }

        #endregion

        #region Click-Events

        private void btnOk_Click(object sender, EventArgs e) {
            Cancel = false;
            SetDataBeforeClose();
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            Cancel = true;
            this.Close();
        }

        #endregion

    }
}
